<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Traits\Filters;

class UserCard extends Model
{

    use Filters;

    protected $table = 'user_member_cards';
    //
    public function user()
    {
        return $this->belongsTo(User::class,'used_by','id');
    }

    public function shop()
    {
        return $this->hasOne(Shop::class,'id','shop_id');
    }

    public function transform_data()
    {
        if($this->shop){
            $use_shop = $this->shop->shop_name;
        }else{
            $use_shop = '';
        }
        if($this->user){
            $used_by = $this->user->nickname;
        }else{
            $used_by = '';
        }
        return [
            'id' => $this->id,
            'use_shop' => $use_shop,
            'card_num' => $this->code,
            'is_used' => $this->is_used,
            'used_by' => $used_by,
        ];
    }


    public function presentCard()
    {
        return $this->hasOne(PresentCard::class,'id','card_id');
    }

    public function transform_present_data()
    {
        switch($this->card_level){
            case '1':
                $level = " 【月卡】";
                $price = 499;
                break;
            case '2':
                $level = " 【季卡】";
                $price = 1300;
                break;
            case '3':
                $level = " 【年卡】";
                $price = 4990;
                break;
            default:
                break;
        }
        return [
            'id' => $this->id,
            'font_color' => $this->presentCard->font_color,
            'image' => config('app.img_url').$this->presentCard->log_image,
            'name' => $this->presentCard->name.$level,
            'price' => $price,
            'code' => $this->code,
            'is_used' => $this->is_used,
        ];
    }

    public function transform_use_log()
    {
        switch($this->card_level){
            case '1':
                $level = " 【月卡】";
                $price = 499;
                break;
            case '2':
                $level = " 【季卡】";
                $price = 1300;
                break;
            case '3':
                $level = " 【年卡】";
                $price = 4990;
                break;
            default:
                break;
        }
        if($this->presentCard){
            $card_name = $this->presentCard->name;
            $image = config('app.img_url').$this->presentCard->log_image;
            $color = $this->presentCard->font_color;
            $card_type = 2;
        }else{
            $card_name = "会员卡";
            $image = '';
            $color = '';
            $card_type = 1;
        }
        return [
            'id' => $this->id,
            'card_type' => $card_type,
            'font_color' => $color,
            'image' => $image,
            'name' => $card_name.$level,
            'price' => $price,
            'used_at' => $this->used_at,
        ];
    }





}
